'use strict';

const wxfunc = require('./wxfunc');

exports.main = async (event, context) => {
	const start = Date.now();
	let res = 'success';
	
	if (event.httpMethod === 'GET') {
		// 验证签名是否一致
		res = wxfunc.validSign(event.queryStringParameters);
	}
	
	if (event.httpMethod === 'POST') {
		if(event.isBase64Encoded){
			// 将base64格式的xml内容转为xml字符串
			event.body = Buffer.from(event.body, 'base64').toString('utf8');
		}
		
		// 微信公众号发生消息 POST 请求
		res = await wxfunc.postMessage(event);
	}
	
	console.log('请求耗时：', Date.now() - start);
	return res;
};
